package ex2.LinkedAndArray;

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;

public class ex4 {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入数字：");
        String a = scanner.nextLine();

        StringTokenizer stringTokenizer = new StringTokenizer(a);
        int[] numbers = new int[stringTokenizer.countTokens()];
        int nGuoKai =0;

        //  将输入的数字存入数组当中。
        while (stringTokenizer.hasMoreTokens())
        {
            numbers[nGuoKai] = Integer.valueOf(stringTokenizer.nextToken());
            nGuoKai++;
        }

        PrintArray(numbers);
        System.out.println();
        System.out.println("个数为：" + nGuoKai);

        //从文件中读取
        File file = new File("D:\\IdeaProjects\\GK20172301_JavaProgramming\\src\\ex2\\LinkedAndArray\\sort.txt");
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String str = "";
        str = bufferedReader.readLine();
        bufferedReader.close();

        StringTokenizer stringTokenizer1 = new StringTokenizer(str);
        int[] numbers1 = new int[stringTokenizer1.countTokens()];
        int y = 0;
        while (stringTokenizer1.hasMoreTokens()) {
            numbers1[y] = Integer.parseInt(stringTokenizer1.nextToken());
            y++;
        }

        int p = numbers1[0];
        int q = numbers1[1];

        // 在第五位插入p
        int[] numbers2 = Insert(numbers,5,p);
        System.out.println("在第五位插入文件中的第一个数：");
        PrintArray(numbers2);
        nGuoKai++;
        System.out.println();
        System.out.println("个数为：" + nGuoKai);
        System.out.println();

        // 在第零位插入q
        int[] numbers3 = Insert(numbers2,0,q);
        System.out.println("在第零位插入文件中的第二个数：");
        PrintArray(numbers3);
        nGuoKai++;
        System.out.println();
        System.out.println("个数为：" + nGuoKai);
        System.out.println();

        // 删除插入的p
        int[] numbers4 = Delete(numbers3,6);
        System.out.println("删除插入的第一个数：");
        PrintArray(numbers4);
        nGuoKai--;
        System.out.println();
        System.out.println("个数为： " + nGuoKai);
        System.out.println();

    }


    public static int[] Insert(int[] nums, int position, int num)
    {
        // 对数组进行扩容
        int[] nums1 = new int[nums.length + 1];
        // 对数组进行扩容，不用新建一个数组。
        // nums = Arrays.copyOf(nums, nums.length + 1);
        //将原数组数据赋值给新数组
        for(int i = 0; i < nums.length; i++)
        {
            nums1[i] = nums[i];
        }
        //将大于position的数据向后移动一位
        for(int j = nums1.length-2;j>position - 1;j--)
        {
            nums1[j+1] = nums1[j];
        }
        //赋值到position位置
        nums1[position] = num;
        return nums1;
    }

    public static int[] Delete(int[]nums, int position)
    {
        // 对数组进行扩容
        int[] nums1 = new int[nums.length - 1];
        int i = 0;
        // 对数组进行扩容，不用新建一个数组。
        // nums = Arrays.copyOf(nums, nums.length - 1);
        //将原数组数据赋值给新数组
        for(int j = 0;j < nums.length ;j++)
        {
            if (position != j) {
                nums1[i] = nums[j];
                i++;
            }
        }
        //赋值到position位置
        return nums1;
    }



    public static void PrintArray(int[] nums)
    {
        for (int i:nums)
        {
            System.out.print(i + " ");
        }
    }
}
